/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChooseForum.java
 *
 * Created on 07/04/2011, 09:49:42
 */

package GUI;


import Business.FrameHandler;
import Domain.DataManager;
import Domain.UserClient.ButtonsFunctions;
import java.awt.Dimension;
import java.awt.Toolkit;
import objects.Frm;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import objects.Trd;
import objects.User;

/**
 *
 * @author Tal Cohen
 */
public class ChooseForum extends javax.swing.JFrame {
    private Vector<Frm> _forums;
    int _manager;
    /** Creates new form ChooseForum */
    public ChooseForum(Vector<Frm> forums,int manager)
    {
        _forums = forums;
        _manager = manager;
        initComponents();

        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        setLocation(screenWidth / 4, screenHeight / 4);

        if(manager == 1){
            VisibleButtons(true,true);
        }
        else{
             VisibleButtons(false,false);
        }
        errorLabel.setVisible(false);
        DefaultListModel forumList = new DefaultListModel();
         for (Iterator<Frm> it = _forums.iterator(); it.hasNext(); ) {
            Frm frm = it.next();  // No downcasting required.
            forumList.addElement(frm.getForumName());
        }
        this.forums.setModel(forumList);
               
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        forums = new javax.swing.JList();
        chooseFourmLabel = new javax.swing.JLabel();
        Continue = new javax.swing.JButton();
        errorLabel = new javax.swing.JLabel();
        AddForum = new javax.swing.JButton();
        manageUsers = new javax.swing.JButton();
        manageFriends = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Choose Forum");

        forums.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jScrollPane1.setViewportView(forums);

        chooseFourmLabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        chooseFourmLabel.setForeground(new java.awt.Color(0, 102, 102));
        chooseFourmLabel.setText("Please Choose A Forum:");

        Continue.setText("Continue");
        Continue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ContinueActionPerformed(evt);
            }
        });

        errorLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        errorLabel.setForeground(new java.awt.Color(255, 51, 51));
        errorLabel.setText("errors will be displayed here");

        AddForum.setLabel("Add Forum");
        AddForum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddForumActionPerformed(evt);
            }
        });

        manageUsers.setText("Manage Users");
        manageUsers.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                manageUsersActionPerformed(evt);
            }
        });

        manageFriends.setText("Manage Friends");
        manageFriends.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                manageFriendsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(errorLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 94, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGap(56, 56, 56)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(AddForum, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(manageFriends, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(manageUsers, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(Continue))
                .addGap(28, 28, 28))
            .addGroup(layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addComponent(chooseFourmLabel)
                .addContainerGap(278, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(69, 69, 69)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(chooseFourmLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(errorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(25, 25, 25))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(manageUsers)
                        .addGap(18, 18, 18)
                        .addComponent(manageFriends)
                        .addGap(18, 18, 18)
                        .addComponent(AddForum)
                        .addGap(63, 63, 63)
                        .addComponent(Continue)
                        .addGap(37, 37, 37))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ContinueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ContinueActionPerformed
        errorLabel.setText("לא בחרת פורום! בחר ונסה שוב");
        if(this.forums.isSelectionEmpty() ){ //if the user did not choose forum
            errorLabel.setVisible(true);
        }
        else{
                ButtonsFunctions.getButtonsFunctions().processGetThreads(forums.getSelectedValue().toString());
                errorLabel.setVisible(false);
                 forums.clearSelection();
                // open new window here new ChooseMove(_control, this,pPilot,cPilot).setVisible(true);
                Vector<Trd> trds = DataManager.getDataManager().getVectorThreads();
                FrameHandler.getFrameHandler().changeCurrentFrame(new Forum(trds));
        }
    }//GEN-LAST:event_ContinueActionPerformed

    private void AddForumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddForumActionPerformed
       JFrame addForum = new AddForum();
       addForum.setVisible(true);
    }//GEN-LAST:event_AddForumActionPerformed

    private void manageUsersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_manageUsersActionPerformed
       ButtonsFunctions.getButtonsFunctions().processGetUsers();
       Vector<User> users = DataManager.getDataManager().getVectorUser();
        FrameHandler.getFrameHandler().changeCurrentFrame(new ManageUsers(users));
    }//GEN-LAST:event_manageUsersActionPerformed

    private void manageFriendsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_manageFriendsActionPerformed
       ButtonsFunctions.getButtonsFunctions().processGetUsers();
       ButtonsFunctions.getButtonsFunctions().processGetFriends();
       Vector<User> users = DataManager.getDataManager().getVectorUser();
       Vector<User> friends = DataManager.getDataManager().getVectorFriends();
       FrameHandler.getFrameHandler().changeCurrentFrame(new ManageFriends(users,friends));
    }//GEN-LAST:event_manageFriendsActionPerformed



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddForum;
    private javax.swing.JButton Continue;
    private javax.swing.JLabel chooseFourmLabel;
    private javax.swing.JLabel errorLabel;
    private javax.swing.JList forums;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton manageFriends;
    private javax.swing.JButton manageUsers;
    // End of variables declaration//GEN-END:variables

    private void VisibleButtons(boolean users, boolean addforum) {
        manageUsers.setVisible(users);
        AddForum.setVisible(addforum);
    }

}
